/**
 * Created with Intellij IDEA.
 * Description:
 * User: wangj
 * Date: 2022-02-25
 * Time: 18:06
 */


import java.util.Random;
import java.util.Scanner;
class Game {
    //     定义12行12列，实际扫雷的区域只有10*10
    static int ROW=12;

    static int COL=12;
    //    定义雷的个数
    static int NUM=20;
    //    0为无炸弹，1为有炸弹
    public static byte BOOM='1';

    public Game(int ROW,int COL,int NUM) {
        this.ROW=ROW;
        this.COL=COL;
        this.NUM=NUM;
    }

    public static void setMines(int[][]mine_board,int row,int col){
        int i=0;
        while (i<NUM){
            Random random=new Random();//默认随机种子是系统时间
            int x=random.nextInt(ROW-2)+1;
            int y=random.nextInt(COL-2)+1;
            if(mine_board[x][y]==BOOM){
                continue;
            }
            mine_board[x][y]=BOOM;
            i++;
        }
    }
    public static void showBoard(int[][]board,int row,int col){
        System.out.print(" ");
        for (int i = 1; i <= row - 2; i++) {
            System.out.printf("%4d", i);
        }
        System.out.println();
        System.out.print(" ");
        for (int k = 1; k <= col - 2; k++) {
            System.out.print("----");
        }
        System.out.println();
        for (int i = 1; i <= ROW - 2; i++) {
            System.out.printf("%2d|", i);
            for (int j = 1; j <= COL - 2; j++) {
                System.out.printf(" %c |", board[i][j]);
            }
            System.out.println();
            for (int k = 1; k <= COL - 2; k++) {
                System.out.print("----");
            }
            System.out.println();
        }
    }
    public static void flag(int[][]board,int a,int b){
        if(b==0){
            System.out.print("请输入插旗坐标<x y> ");
            Scanner sc=new Scanner(System.in);
            int x=sc.nextInt();int y=sc.nextInt();
            board[x][y]='$';
        }
        else{
            while(true){
                System.out.print("请输入取消插旗坐标<x y> ");
                Scanner sc=new Scanner(System.in);
                int i=sc.nextInt();int j=sc.nextInt();
                if(board[i][j]=='$'){
                    board[i][j]='*';
                    break;
                }
                else{
                    System.out.println("输入坐标有误！");
                    continue;

                }
            }
        }
    }
    public static int countMines(int[][]board,int x,int y){
        return board[x - 1][y - 1] + board[x - 1][y]
                +board[x - 1][y + 1] + board[x][y - 1]
                + board[x][y + 1] + board[x + 1][y - 1]
                +board[x + 1][y] + board[x + 1][y + 1] - 8 *'0';
    }
    public static void game(){
        int[][] show_board=new int[ROW][COL];
        int[][] mine_board=new int[ROW][COL];
        for (int i = 0; i < ROW; i++) {
            for (int j = 0; j < COL; j++) {
                show_board[i][j]='*';
                mine_board[i][j]='0';
            }
        }
        setMines(mine_board,ROW,COL);
        int tot=(ROW-2)*(COL-2)-NUM;
        int clear=0;
        while(true){
            showBoard(show_board,ROW,COL);
            System.out.print("请输扫雷入坐标<x y> ");
            Scanner sc=new Scanner(System.in);
            int x=sc.nextInt();int y=sc.nextInt();
            if((x==0&&y==0)||(x==0&&y==1)){
                flag(show_board,x,y);
                continue;
            }
            if(!(x>=1&&x<=ROW-2&&y>=1&&y<=COL-2)){
                System.out.println("扫雷的位置有问题！");
                continue;
            }
            if(show_board[x][y]!='*'){
                System.out.println("扫雷的位置上已经排除！");
                continue;
            }
            if(mine_board[x][y]=='1'){
                System.out.println("不好意思，你被炸死了！");
                break;
            }
            else{
                int count=countMines(mine_board,x,y);
                show_board[x][y]=count+'0';
                clear++;
            }
            if (clear >= tot) {
                System.out.println("恭喜你，游戏胜利！");
                break;
            }
        }

    }

}
class Menu{
    public static void menu(){
        System.out.println("####################");
        System.out.println("#####  1.Play  #####");
        System.out.println("#####  2.Exit  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }

}
class Level{
    public static void Level(){
        System.out.println("####################");
        System.out.println("#####  level1  #####");
        System.out.println("#####  level2  #####");
        System.out.println("#####  level3  #####");
        System.out.println("####################");
        System.out.print("Please Select> ");
    }
    public void level(){
        Game.game();
    }
}
class Level1 extends Level{
    @Override
    public  void level(){
        Game game=new Game(7,7,5);
        Game.game();
    }

}
class Level2 extends Level{
    @Override
    public  void level(){
        Game game=new Game(9,9,10);
        Game.game();
    }

}
class Level3 extends Level{
    @Override
    public  void level(){
        Game game=new Game(12,12,20);
        Game.game();
    }

}
class MineSweep{
    public static void fun(Level Level){
        Level.level();
    }
    public void game() {
        boolean quit = true;
        while (quit) {
            Menu.menu();
            Scanner sc = new Scanner(System.in);
            int select = sc.nextInt();
            switch (select) {
                case 1:
                    Level.Level();
                    Scanner scn = new Scanner(System.in);
                    int level = sc.nextInt();
                    switch (level) {
                        case 1:
                            fun(new Level1());
//                            Level a = new Level1();
//                            a.level();
                            break;
                        case 2:
                            fun(new Level2());
                            break;
                        case 3:
                            fun(new Level3());
                            break;
                        default:
                            System.out.println("输入有误！");
                            break;
                    }
                    break;
                case 2:
                    System.out.println("Bye Bye!");
                    quit = false;
                    break;
                default:
                    System.out.println("输入有误，请重新输入！");
                    break;

            }

        }
    }
}

public class MineSweepPro {
    public static void main(String[] args) {
        MineSweep mineSweep=new MineSweep();
        mineSweep.game();
    }
}




